<?php

class PageLimitManager
{
    protected $_searchKey ;     // search key
    protected $_page ;          // 頁數
    protected $_pageShowCount ; // 每頁顯示數量
    protected $_rowCount ;      // 資料總筆數
    protected $_gap = 5 ;       // 間距
    protected $_baseUrl ;

    /**
     *  init
     */
    public function __construct()
    {
        $this->setPage( 1 );
        $this->setPageShowCount( BaseMapper::DEFAULT_ROW_COUNT );
    }

    /**
     *  設定每頁幾筆資料
     */
    public function setPageShowCount( $pageShowCount )
    {
        $this->_pageShowCount = (int) $pageShowCount;
    }
    public function getPageShowCount()
    {
        return $this->_pageShowCount;
    }

    /**
     *  設定資料總筆數
     */
    public function setRowCount( $rowCount )
    {
        $this->_rowCount = (int) $rowCount;
    }
    public function getRowCount()
    {
        return $this->_rowCount;
    }

    /**
     *  設定現在在第幾頁
     */
    public function setPage( $page )
    {
        if( $page<1 ) {
            $page = 1;
        }
        $this->_page = (int) $page;
    }
    public function getPage()
    {
        return $this->_page;
    }

    /**
     *  設定 search key
     */
    public function setSearchKey( $searchKey )
    {
        $this->_searchKey = $searchKey;
    }
    public function getSearchKey()
    {
        return $this->_searchKey;
    }

    // ====================================================================================================

    /**
     *  需要 url manager 來產生網址
     */
    function setBaseUrl( $baseUrl )
    {
        $this->_baseUrl = $baseUrl;
    }

    // ====================================================================================================

    /**
     *  產生基本樣式
     */
    public function render()
    {

        $start = $this->_page - $this->_gap;
        $end   = $this->_page + $this->_gap;
        if( $start<1 ) {
            $start=1;
        }

        $maxPage = Ceil( $this->_rowCount / $this->_pageShowCount );
        if( $end>$maxPage ) {
            $end=$maxPage;
        }

        if( $this->_rowCount <= $this->_pageShowCount ) {
            return '';
        }

        $html = '<div class="pager">';
        if( 1 != $this->_page ) {
            $html .= '<a class="pagerLinkPrev" href="'. $this->_generateUrl($this->_page-1) .'"> &laquo; Prev </a>';
        }
        else {
            //$html .= '<span class="pagerLinkPrev"> &laquo; Prev </span>';
        }

        for( $i=$start; $i<=$end ; $i++ ) {
            if( $this->_page == $i ) {
                $html .= '<span class="pagerCurrent">'. $i .'</span>';
            }
            else {
                $html .= '<a class="pagerLink" href="'. $this->_generateUrl($i) .'">'. $i .'</a>';
            }
        }

        if( $maxPage != $this->_page ) {
            $html .= '<a class="pagerLinkNext" href="'. $this->_generateUrl($this->_page+1) .'"> Next &raquo; </a>';
        }
        else {
            //$html .= '<span class="pagerLinkNext"> Next &raquo; </span>';
        }

        $html .= '</div>';
        return $html;
    }

    /*
        拼出 url
    */
    private function _generateUrl( $page='' )
    {
        if( $this->_searchKey ) {
            return $this->_baseUrl .'/'. $page .'/'. $this->_searchKey;
        }
        elseif( $page>1 ) {
            return $this->_baseUrl .'/'. $page;
        }
        else {
            return $this->_baseUrl;
        }
    }






    /**
     *  產生 blog 基本樣式
     */
    public function blogRender()
    {

        $start = $this->_page - $this->_gap;
        $end   = $this->_page + $this->_gap;
        if( $start<1 ) {
            $start=1;
        }

        $maxPage = Ceil( $this->_rowCount / $this->_pageShowCount );
        if( $end>$maxPage ) {
            $end=$maxPage;
        }

        if( $this->_rowCount <= $this->_pageShowCount ) {
            return '';
        }

        $html = '<div class="pager">';
        if( 1 != $this->_page ) {
            $html .= '<a class="pagerLinkPrev" href="'. $this->_generateBlogUrl($this->_page-1) .'"> &laquo; Prev </a>';
        }
        else {
            //$html .= '<span class="pagerLinkPrev"> &laquo; Prev </span>';
        }

        for( $i=$start; $i<=$end ; $i++ ) {
            if( $this->_page == $i ) {
                $html .= '<span class="pagerCurrent">'. $i .'</span>';
            }
            else {
                $html .= '<a class="pagerLink" href="'. $this->_generateBlogUrl($i) .'">'. $i .'</a>';
            }
        }

        if( $maxPage != $this->_page ) {
            $html .= '<a class="pagerLinkNext" href="'. $this->_generateBlogUrl($this->_page+1) .'"> Next &raquo; </a>';
        }
        else {
            //$html .= '<span class="pagerLinkNext"> Next &raquo; </span>';
        }

        $html .= '</div>';
        return $html;
    }

    /*
        拼出 blog url
    */
    private function _generateBlogUrl( $page='' )
    {
        if( $this->_searchKey ) {
            return $this->_baseUrl .'/page/'. $page .'/'. $this->_searchKey;
        }
        elseif( $page>1 ) {
            return $this->_baseUrl .'/page/'. $page;
        }
        else {
            return $this->_baseUrl;
        }
    }



}
